package com.flynn.rock.system.repository;

import com.flynn.rock.system.entity.SysMessageEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;

public interface SysMessageRepository extends JpaRepository<SysMessageEntity, Long>, JpaSpecificationExecutor<SysMessageEntity> {

    @Query("select count(*) from SysMessageEntity as t where t.toUserId = :userId and t.readMessageTime is null ")
    Long countUnreadMessageNum(@Param("userId") Long userId);

    @Modifying
    @Query("update SysMessageEntity as t set t.readMessageTime = :readMessageTime where t.toUserId = :userId")
    void readAllUserMessage(@Param("userId") Long userId, @Param("readMessageTime") LocalDateTime readMessageTime);
}
